<?php
namespace App\Utils;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

/**
 * 表创建迁移
 * Class CreateMigration
 * @package App\Utils
 */
abstract class CreateMigration extends Migration
{
    protected $tableName;
    protected $comment;

    abstract function columns(Blueprint $table);

    abstract function tableInfo();

    public function __construct()
    {
        list($name, $comment) = $this->tableInfo();
        $this->tableName = $name;
        $this->comment = $comment;
    }

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create($this->tableName, function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->bigIncrements('id')->comment('自增ID');
            $this->columns($table);
            $table->unsignedInteger('created_at')->default(0)->comment('创建时间');
            $table->unsignedInteger('updated_at')->default(0)->comment('更新时间');
            $table->unsignedInteger('deleted_at')->default(0)->comment('删除时间0 未删除');
        });
        DB::statement("ALTER TABLE `$this->tableName` comment '$this->comment'");
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }
}
